package com.asset.dispoal.business.bsAssetInstitutionBatch.service.commom;

import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.entity.BsAssetInstitutionBatch;
import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.mapper.BsAssetInstitutionBatchMapper;
import com.asset.dispoal.business.bsAssetInstitutionBatch.dto.BsAssetInstitutionBatchRequest;
import com.asset.dispoal.business.bsAssetInstitutionBatch.dto.BsAssetInstitutionBatchResponse;
import com.asset.dispoal.business.bsAssetInstitutionBatch.service.converter.BsAssetInstitutionBatchConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/24$
 */
@Component
public class BsAssetInstitutionBatchQueryPage implements BaseCommand<BsAssetInstitutionBatchRequest, Result> {

    @Autowired
    BsAssetInstitutionBatchMapper bsAssetInstitutionBatchMapper;

    @Override
    public Result<PageVO<BsAssetInstitutionBatchResponse>> execute(BsAssetInstitutionBatchRequest bsAssetInstitutionBatchRequest) {
        BsAssetInstitutionBatch bsAssetInstitutionBatch = BsAssetInstitutionBatchConverter.INSTANCE.resToDo(bsAssetInstitutionBatchRequest);
        bsAssetInstitutionBatch.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));

        int count = bsAssetInstitutionBatchMapper.count(bsAssetInstitutionBatch);
        Page<BsAssetInstitutionBatch> page = PagingQuery.getPage(count, bsAssetInstitutionBatch);
        PageVO<BsAssetInstitutionBatchResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<BsAssetInstitutionBatch> batchList = bsAssetInstitutionBatchMapper.page(page);
            List<BsAssetInstitutionBatchResponse> bsAssetInstitutionBatchResponses = BsAssetInstitutionBatchConverter.INSTANCE.doToRes(batchList);
            pageVO.setResult(bsAssetInstitutionBatchResponses);
        }
        return Result.ok(pageVO);
    }
}
